﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AdmLaboratorios.DAO;
using AdmLaboratorios.DTO;
using System.Data.SqlClient;
using System.Data;

namespace AdmLaboratorios.Business
{
    public static class InformesLaboratorio
    {
        static InformeLaboratorioDAO daoCC = DAOFactory.Get<InformeLaboratorioDAO>();

        public static InformeLaboratorioDTO Save(InformeLaboratorioDTO cc)
        {
            try
            {
                using (SqlTransaction tran = daoCC.IniciarTransaccion())
                {
                    cc = daoCC.Create(cc, tran);
                    daoCC.Log(LogDTO.eAcciones.Alta, "Alta de CC", cc.Id, tran);

                    Stock.ChangeState(cc, tran);
                    daoCC.CommitTransaccion(tran);

                    return cc;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Save", ex);
            }
        }


        public static DataTable Consulta(string estado, DateTime desde, DateTime hasta)
        {
            if (estado != eStockEstado.TODO.ToString())
                return daoCC.Consulta(string.Format("CC.Resultado = '{0}' AND CC.Fecha BETWEEN '{1}' AND '{2}'", 
                    estado.Trim().ToUpper(), 
                    desde.ToString("yyyyMMdd"), 
                    hasta.ToString("yyyyMMdd")));
            else
                return daoCC.Consulta(string.Format("CC.Fecha BETWEEN '{0}' AND '{1}'",
                    desde.ToString("yyyyMMdd"),
                    hasta.ToString("yyyyMMdd")));
        }

        public static InformeLaboratorioDTO Get(int id)
        {
            return daoCC.Read(id);
        }
    }
}
